package top.lwaiting.java.ai.langchain4j.config;

import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SeparateChatAssistantConfig {

    @Bean
    public ChatMemoryProvider chatMemoryProvider() {
        // 为每个用户的新聊天或者不同的用户区分聊天记忆
        return memoryId -> MessageWindowChatMemory.builder().id(memoryId).maxMessages(10).build();
    }

    /**
     * 自定义聊天记忆 Bean
     * 指定聊天记忆存储的实现类
     * @return {ChatMemoryProvider}
     */
    @Bean
    public ChatMemoryProvider chatMemoryProviderCustom() {
        // 为每个用户的新聊天或者不同的用户区分聊天记忆
        return memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .chatMemoryStore(new InMemoryChatMemoryStore())
                .maxMessages(10)
                .build();
    }

    /**
     * 自定义聊天记忆 Bean
     * 指定聊天记忆存储的实现类，自定义实现，将聊天记忆存储到数据库中进行持久化
     * @return {ChatMemoryProvider}
     */
    @Bean
    public ChatMemoryProvider chatMemoryProviderCustom2DB() {
        // 为每个用户的新聊天或者不同的用户区分聊天记忆
        return memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .chatMemoryStore(new InMemoryChatMemoryStore())
                .maxMessages(10)
                .build();
    }
}
